package c.j.a.d.a;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings.Secure;
import android.text.TextUtils;
import c.j.a.d.b.n.d0;
import com.huawei.agconnect.exception.AGCServerException;
import com.ss.android.socialbase.appdownloader.view.JumpUnknownSourceActivity;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.json.JSONArray;
import org.json.JSONObject;

/* compiled from: AntiHijackUtils */
public class g {
    public static c a;
    public static final HashMap<String, m> b = new HashMap();
    public static a c;

    /* compiled from: AntiHijackUtils */
    public interface b {
        boolean a(Context context);
    }

    /* compiled from: AntiHijackUtils */
    public interface c {
    }

    /* compiled from: AntiHijackUtils */
    public static class d implements Callable<Boolean> {
        public final Context a;
        public final b b;
        public final Handler c;
        public final long d;

        public d(Handler handler, Context context, b bVar, long j) {
            this.a = context;
            this.b = bVar;
            this.c = handler;
            this.d = j;
        }

        public Object call() throws Exception {
            try {
                b bVar = this.b;
                if (bVar != null) {
                    long j = this.d;
                    if (j > 0) {
                        if (j <= 10000) {
                            boolean z = false;
                            Context context = this.a;
                            if (context != null) {
                                z = bVar.a(context);
                            }
                            Message obtain = Message.obtain();
                            if (z) {
                                obtain.what = 2;
                                this.c.sendMessage(obtain);
                            } else {
                                obtain.what = 1;
                                this.c.sendMessageDelayed(obtain, this.d);
                            }
                        }
                    }
                }
            } catch (Throwable unused) {
            }
            return Boolean.FALSE;
        }
    }

    /* compiled from: Constants */
    public class f {
        public static final String a;

        static {
            StringBuilder stringBuilder = new StringBuilder();
            String str = VERSION.RELEASE;
            int i = 1;
            int isEmpty = TextUtils.isEmpty(str) ^ 1;
            int isEmpty2 = TextUtils.isEmpty(Build.ID) ^ 1;
            if (!"REL".equals(VERSION.CODENAME) || TextUtils.isEmpty(Build.MODEL)) {
                i = 0;
            }
            stringBuilder.append("AppDownloader");
            if (isEmpty != 0) {
                stringBuilder.append("/");
                stringBuilder.append(str);
            }
            stringBuilder.append(" (Linux; U; Android");
            String str2 = " ";
            if (isEmpty != 0) {
                stringBuilder.append(str2);
                stringBuilder.append(str);
            }
            if (!(i == 0 && isEmpty2 == 0)) {
                stringBuilder.append(";");
                if (i != 0) {
                    stringBuilder.append(str2);
                    stringBuilder.append(Build.MODEL);
                }
                if (isEmpty2 != 0) {
                    stringBuilder.append(" Build/");
                    stringBuilder.append(Build.ID);
                }
            }
            stringBuilder.append(")");
            a = stringBuilder.toString();
        }
    }

    /* compiled from: AntiHijackUtils */
    public static class a implements c.j.a.d.b.a.a.b {
        public final e a;
        public final int b;
        public JSONObject c;

        public a(Context context, Intent intent, JSONObject jSONObject, b bVar) {
            this.c = jSONObject;
            int optInt = jSONObject.optInt("query_interval", 1000);
            this.b = optInt;
            this.a = new e(context, intent, bVar, (long) optInt);
        }

        public void b() {
            if (!this.a.g) {
                Message obtain = Message.obtain();
                obtain.what = 2;
                this.a.d.sendMessage(obtain);
            }
            c.j.a.d.b.a.a aVar = c.j.a.d.b.a.a.c.a;
            synchronized (aVar.b) {
                aVar.b.remove(this);
            }
            g.c = null;
        }

        public void c() {
            if (VERSION.SDK_INT < 29) {
                int optInt = this.c.optInt("time_out_second", 10);
                Message obtain = Message.obtain();
                obtain.what = 1;
                this.a.d.sendMessage(obtain);
                if (optInt > 0 && optInt < 60) {
                    obtain = Message.obtain();
                    obtain.what = 2;
                    this.a.d.sendMessageDelayed(obtain, (long) (optInt * 1000));
                }
            }
        }
    }

    /* compiled from: AntiHijackUtils */
    public static class e implements c.j.a.d.b.m.f.a {
        public final Context a;
        public final Intent b;
        public final b c;
        public final Handler d;
        public final long e;
        public Future<Boolean> f;
        public boolean g = false;

        public e(Context context, Intent intent, b bVar, long j) {
            this.a = context;
            this.b = intent;
            this.c = bVar;
            this.d = new c.j.a.d.b.m.f(Looper.getMainLooper(), this);
            this.e = j;
        }

        public void a(Message message) {
            int i = message.what;
            if (i == 1) {
                long j = this.e;
                if (j > 0 && j <= 10000) {
                    this.f = c.j.a.d.b.e.b.q().submit(new d(this.d, this.a, this.c, this.e));
                }
            } else if (i == 2) {
                this.d.removeMessages(2);
                this.d.removeMessages(1);
                Future future = this.f;
                if (future != null) {
                    future.cancel(true);
                }
                if (!this.g) {
                    g.m(this.a, this.b);
                    this.g = true;
                }
            }
        }
    }

    public static m a(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        try {
            String[] split = str.split(",");
            if (split != null) {
                for (CharSequence charSequence : split) {
                    if (!TextUtils.isEmpty(charSequence)) {
                        m l = l(charSequence);
                        if (l != null) {
                            return l;
                        }
                    }
                }
            }
        } catch (Throwable unused) {
        }
        return null;
    }

    public static String b(Throwable th) {
        String th2 = th.toString();
        return th2.length() > 800 ? th2.substring(0, AGCServerException.UNKNOW_EXCEPTION) : th2;
    }

    public static void c(Context context, Intent intent, JSONObject jSONObject, b bVar) {
        if (c != null) {
            c.j.a.d.b.a.a aVar = c.j.a.d.b.a.a.c.a;
            a aVar2 = c;
            synchronized (aVar.b) {
                aVar.b.remove(aVar2);
            }
            c = null;
        }
        c = new a(context, intent, jSONObject, bVar);
        c.j.a.d.b.a.a.c.a.a(c);
    }

    public static void d(d dVar, int i) {
        int i2 = dVar.b;
        if (i2 != -1) {
            dVar.b = (i2 * 10) + i;
        } else {
            dVar.b = i;
        }
    }

    public static boolean e(Context context) {
        int i = VERSION.SDK_INT;
        if (context == null) {
            return true;
        }
        try {
            if (d0.d() && i < 26) {
                return q(context);
            }
            if (i >= 26 && context.getApplicationInfo().targetSdkVersion >= 26) {
                return r(context);
            }
            return true;
        } catch (Throwable unused) {
        }
    }

    public static boolean f(Context context, Intent intent, JSONObject jSONObject, d dVar) {
        if (context != null) {
            int optInt = jSONObject.optInt("max_jump_count", 0);
            if (optInt <= 0) {
                return false;
            }
            SharedPreferences sharedPreferences = context.getSharedPreferences("sp_anti_hijack_config", 0);
            String str = "jump_unknown_source_count";
            int i = sharedPreferences.getInt(str, 0);
            if (i < optInt && !e(context)) {
                sharedPreferences.edit().putInt(str, i + 1).apply();
                Intent intent2 = new Intent(context, JumpUnknownSourceActivity.class);
                intent2.addFlags(268435456);
                intent2.putExtra("intent", intent);
                intent2.putExtra("config", jSONObject.toString());
                try {
                    g(context, intent2, false);
                    dVar.b = 0;
                    return true;
                } catch (Throwable th) {
                    dVar.b = 1;
                    StringBuilder g = c.b.a.a.a.g("tryShowUnknownSourceDialog");
                    g.append(b(th));
                    dVar.c = g.toString();
                }
            }
        }
        return false;
    }

    public static boolean g(Context context, Intent intent, boolean z) {
        if (context == null || intent == null) {
            return false;
        }
        String str = "start_only_for_android";
        if (z) {
            try {
                intent.putExtra(str, true);
                context.startActivity(intent);
                return true;
            } catch (Throwable unused) {
                return false;
            }
        }
        intent.putExtra(str, true);
        context.startActivity(intent);
        return true;
    }

    public static boolean h(File file, com.ss.android.socialbase.downloader.g.c cVar) {
        String path = file.getPath();
        JSONObject l = c.j.a.d.b.l.a.d(cVar.g0()).l("anti_hijack_dir");
        File file2 = null;
        CharSequence optString = l != null ? l.optString("ins_desc") : null;
        if (!(TextUtils.isEmpty(optString) || TextUtils.isEmpty(optString))) {
            file2 = new File(c.b.a.a.a.d(c.b.a.a.a.g(path), File.separator, optString));
        }
        if (file2 != null) {
            try {
                if (file2.createNewFile()) {
                    file2.deleteOnExit();
                }
            } catch (IOException unused) {
                return false;
            }
        }
        return true;
    }

    public static boolean i(JSONArray jSONArray) {
        if (jSONArray == null) {
            return true;
        }
        int length = jSONArray.length();
        if (length == 0) {
            return true;
        }
        boolean z = false;
        for (int i = 0; i < length; i++) {
            JSONObject optJSONObject = jSONArray.optJSONObject(i);
            if (optJSONObject != null) {
                String optString = optJSONObject.optString("package_names");
                JSONArray optJSONArray = optJSONObject.optJSONArray("version_white");
                JSONArray optJSONArray2 = optJSONObject.optJSONArray("version_black");
                if (TextUtils.isEmpty(optString) || (optJSONArray == null && optJSONArray2 == null)) {
                    return false;
                }
                String[] split = optString.split(",");
                if (split != null) {
                    for (String l : split) {
                        m l2 = l(l);
                        if (l2 != null) {
                            String str = l2.e;
                            int i2 = l2.f;
                            StringBuilder stringBuilder = new StringBuilder();
                            stringBuilder.append(i2);
                            stringBuilder.append("_");
                            stringBuilder.append(str);
                            str = stringBuilder.toString();
                            z = optJSONArray == null || optJSONArray.length() <= 0 ? optJSONArray2 == null || optJSONArray2.length() <= 0 || j(optJSONArray2, str) : !j(optJSONArray, str);
                            if (!z) {
                                return false;
                            }
                        }
                    }
                    continue;
                } else {
                    continue;
                }
            }
        }
        return z;
    }

    public static boolean j(JSONArray jSONArray, String str) {
        if (str != null) {
            int length = jSONArray.length();
            for (int i = 0; i < length; i++) {
                if (str.equalsIgnoreCase(jSONArray.optString(i).trim())) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean k(JSONObject jSONObject) {
        String optString = jSONObject.optString("device_rom");
        if (TextUtils.isEmpty(optString)) {
            return true;
        }
        String g = d0.g();
        if (!TextUtils.isEmpty(g) && optString.toLowerCase().contains(g.toLowerCase())) {
            return true;
        }
        return false;
    }

    /* JADX WARNING: Removed duplicated region for block: B:25:? A:{SYNTHETIC, RETURN} */
    /* JADX WARNING: Removed duplicated region for block: B:21:0x0059  */
    public static c.j.a.d.a.m l(java.lang.String r13) {
        /*
        r0 = b;
        r1 = r0.containsKey(r13);
        r2 = 0;
        if (r1 == 0) goto L_0x0013;
    L_0x0009:
        r13 = r0.get(r13);
        r13 = (c.j.a.d.a.m) r13;
        if (r13 == 0) goto L_0x005a;
    L_0x0011:
        r2 = r13;
        goto L_0x005a;
    L_0x0013:
        r0 = c.j.a.d.b.e.b.a();	 Catch:{ NameNotFoundException -> 0x004d }
        r0 = r0.getPackageManager();	 Catch:{ NameNotFoundException -> 0x004d }
        if (r0 != 0) goto L_0x001e;
    L_0x001d:
        goto L_0x0025;
    L_0x001e:
        r1 = 0;
        r3 = r0.getPackageInfo(r13, r1);	 Catch:{ NameNotFoundException -> 0x004d }
        if (r3 != 0) goto L_0x0027;
    L_0x0025:
        r0 = r2;
        goto L_0x0052;
    L_0x0027:
        r4 = r3.applicationInfo;	 Catch:{ NameNotFoundException -> 0x004d }
        r6 = r3.packageName;	 Catch:{ NameNotFoundException -> 0x004d }
        r5 = r4.loadLabel(r0);	 Catch:{ NameNotFoundException -> 0x004d }
        r7 = r5.toString();	 Catch:{ NameNotFoundException -> 0x004d }
        r8 = r4.loadIcon(r0);	 Catch:{ NameNotFoundException -> 0x004d }
        r9 = r4.sourceDir;	 Catch:{ NameNotFoundException -> 0x004d }
        r10 = r3.versionName;	 Catch:{ NameNotFoundException -> 0x004d }
        r11 = r3.versionCode;	 Catch:{ NameNotFoundException -> 0x004d }
        r0 = r4.flags;	 Catch:{ NameNotFoundException -> 0x004d }
        r3 = 1;
        r0 = r0 & r3;
        if (r0 == 0) goto L_0x0045;
    L_0x0043:
        r12 = 1;
        goto L_0x0046;
    L_0x0045:
        r12 = 0;
    L_0x0046:
        r0 = new c.j.a.d.a.m;	 Catch:{ NameNotFoundException -> 0x004d }
        r5 = r0;
        r5.m2350init(r6, r7, r8, r9, r10, r11, r12);	 Catch:{ NameNotFoundException -> 0x004d }
        goto L_0x0052;
    L_0x004d:
        r0 = move-exception;
        r0.printStackTrace();
        goto L_0x0025;
    L_0x0052:
        r1 = b;
        r1.put(r13, r0);
        if (r0 == 0) goto L_0x005a;
    L_0x0059:
        r2 = r0;
    L_0x005a:
        return r2;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.j.a.d.a.g.l(java.lang.String):c.j.a.d.a.m");
    }

    public static boolean m(Context context, Intent intent) {
        return g(context, intent, true);
    }

    public static boolean n(JSONObject jSONObject) {
        int optInt = jSONObject.optInt("min_os_api", -1);
        boolean z = true;
        if (optInt < 0) {
            return true;
        }
        if (VERSION.SDK_INT < optInt) {
            z = false;
        }
        return z;
    }

    public static d o(JSONObject jSONObject) {
        d dVar = new d();
        String optString = jSONObject.optString("type");
        dVar.a = optString;
        try {
            if (i(jSONObject.optJSONArray("device_requirements"))) {
                if ("jump_file_manager_custom".equals(optString)) {
                    optString = "custom";
                    dVar.e = optString;
                    if (c.j.a.d.a.c.d.b(c.j.a.d.b.e.b.a(), optString, jSONObject)) {
                        dVar.b = 0;
                        return dVar;
                    }
                    d(dVar, 3);
                } else {
                    optString = jSONObject.optString("device_plans");
                    dVar.e = optString;
                    if (!TextUtils.isEmpty(optString)) {
                        for (String b : optString.split(",")) {
                            if (c.j.a.d.a.c.d.b(c.j.a.d.b.e.b.a(), b, jSONObject)) {
                                dVar.b = 0;
                                return dVar;
                            }
                            d(dVar, 3);
                        }
                    }
                }
                return dVar;
            }
            d(dVar, 2);
            return dVar;
        } catch (Throwable th) {
            StringBuilder g = c.b.a.a.a.g("checkJumpFileManagerConfig");
            g.append(b(th));
            dVar.c = g.toString();
            d(dVar, 4);
            return dVar;
        }
    }

    public static d p(JSONObject jSONObject) {
        d dVar = new d();
        dVar.a = jSONObject.optString("type");
        try {
            if (i(jSONObject.optJSONArray("device_requirements"))) {
                dVar.b = 0;
                return dVar;
            }
            d(dVar, 2);
            return dVar;
        } catch (Throwable th) {
            StringBuilder g = c.b.a.a.a.g("checkCustomSavePathConfig");
            g.append(b(th));
            dVar.c = g.toString();
            d(dVar, 4);
            return dVar;
        }
    }

    public static boolean q(Context context) {
        boolean z = true;
        if (context == null) {
            return true;
        }
        try {
            if (Secure.getInt(context.getContentResolver(), "install_non_market_apps", 1) <= 0) {
                z = false;
            }
        } catch (Throwable unused) {
        }
        return z;
    }

    public static boolean r(Context context) {
        if (context == null) {
            return true;
        }
        try {
            return context.getPackageManager().canRequestPackageInstalls();
        } catch (Throwable unused) {
            return true;
        }
    }
}
